import java.awt.Image;
import javax.swing.text.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.imageio.*;
import javax.print.attribute.AttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.ImageView;
import javax.swing.text.html.parser.ParserDelegator;
import java.net.URLConnection;
import java.util.Scanner;
import java.lang.*; 


import java.io.InputStream;


import java.lang.*;
public class GetPictures{
	public void downloadPicture(URL url){
		BufferedImage img = null;
		try{
			img = ImageIO.read(url);
			String s = url.toString();
			String ext = s.substring(s.lastIndexOf('.') + 1);
			String nameOfPicture = s.substring(s.lastIndexOf('/') + 1);
			
			if(img != null){
				
				File file = new File("pictures");
				if(!(file.exists() && file.isDirectory())){
					file.mkdir();
					
				}
				ImageIO.write(img, ext, new File("pictures" + File.separator + nameOfPicture));
			}
		}
		catch(IOException e){
			e.printStackTrace();
			
		}
		
	}
	
	
	
	public void getURlPicturesDownloads(String stringUrl){
		try {
		URL url = new URL(stringUrl);
		InputStream is =url.openStream();
		InputStreamReader isr = new InputStreamReader(is);
		//char[] buff = new char[100];
//		Scanner scan = new Scanner(isr);
//		while(scan.hasNext()){
//			String s = scan.nextLine();
//			System.out.println(s);
//		}
		HTMLEditorKit htmlKit = new HTMLEditorKit();
        HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
        HTMLEditorKit.Parser parser = new ParserDelegator();
        HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
        parser.parse(isr, callback, true);
        HTMLDocument.Iterator it = htmlDoc.getIterator(HTML.Tag.IMG);
        while(it.isValid()){
        	javax.swing.text.AttributeSet attr = (javax.swing.text.AttributeSet) it.getAttributes();
           
        	String s = attr.getAttribute(HTML.Attribute.SRC).toString();
        	this.downloadPicture(new URL(s));
            it.next();
        }
   		}
		catch(Throwable t){
			t.printStackTrace();
		}
		
	}
}